basepath=$(realpath $(dirname $0))
cd $basepath
source ${basepath}/pre.sh
pre_exit_code=$?
echo "${basepath}/pre.sh exit code: $pre_exit_code"
if [ ! $pre_exit_code == 0 ]; then
   echo "please ensure the pre exit 0, for flow safe."
   exit
else
   echo "${basepath}/pre.sh exit 0, normal, contine..."
fi

user_downloads_path=/home/${USER}/Downloads
yum_axelget_url=https://github.com/crook/yum-axelget/archive/v1.0.5.1.zip
echo "The yum_axelget_url is: ${yum_axelget_url}"
yum_axelget_basename=`basename ${yum_axelget_url}`
yum_axelget_targz_path=${user_downloads_path}/yum-axelget-${yum_axelget_basename}
echo "The yum_axelget_targz_path is: ${yum_axelget_targz_path}"
if [ -f $yum_axelget_targz_path ]; then
    rm $yum_axelget_targz_path
fi
wget $yum_axelget_url -O $yum_axelget_targz_path
chown ${USER}:${USER} $yum_axelget_targz_path
yum_axelget_targz_extracted_basename=$(basename $yum_axelget_targz_path)
echo "The yum_axelget_targz_extracted_basename is: $yum_axelget_targz_extracted_basename"
yum_axelget_targz_extracted_basename=$(basename $yum_axelget_targz_extracted_basename .zip)
echo "The yum_axelget_targz_extracted_basename is: $yum_axelget_targz_extracted_basename"
yum_axelget_targz_extracted_basename=$(echo $yum_axelget_targz_extracted_basename | sed "s/v//")
echo "The yum_axelget_targz_extracted_basename is: $yum_axelget_targz_extracted_basename"
yum_axelget_targz_extracted_path=/lopt/$yum_axelget_targz_extracted_basename
echo "The yum_axelget_targz_extracted_basename is: $yum_axelget_targz_extracted_basename"
if [ $yum_axelget_targz_extracted_path == '/lopt/' ]; then
    echo "invalid yum_axelget_targz_extracted_path got: ${yum_axelget_targz_extracted_path}"
    exit 2
fi
echo "the yum_axelget_targz_extracted_path is: $yum_axelget_targz_extracted_path"
if [[ -d $yum_axelget_targz_extracted_path ]]; then
    mv $yum_axelget_targz_extracted_path /tmp/$yum_axelget_targz_extracted_basename_backup_$(date +%Y%m%d_%H%m%S)
fi
unzip $yum_axelget_targz_path -d /lopt/
if [ ! $? == 0 ]; then
    echo "FAILD: unzip $yum_axelget_targz_path -d /lopt/; # check the ${yum_axelget_targz_path} is just the unzip format."
else
    cp ${yum_axelget_targz_extracted_path}/axelget.conf /etc/yum/pluginconf.d/
    cp ${yum_axelget_targz_extracted_path}/axelget.py  /usr/lib/yum-plugins/
    sed -i /plugins=0/s/plugins=0/plugins=1/ /etc/yum.conf
    echo "Ensured the the plugins=1 in /etc/yum.conf, is it right? Read below:"
    echo "####################################################################"
    cat /etc/yum.conf
    echo "####################################################################"
    sleep 2
fi


# when debug==1, not excute yum commands, in order run faster.
echo "Const varaible DEBUG is: ${DEBUG}"
if [ ! $DEBUG == 1 ]; then 
    #yum -y install epel-release -y
    rpm -i http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    if [ $? == 0 ]; then
        echo "rmp epel-release-latest-7.noarch.rpm successed."
    else
        echo "rmp epel-release-latest-7.noarch.rpm failed."
    fi
    echo "yuming makecache..."
    yum makecache
    is_yum_plugin_fastestmirror_installed=$(rpm -qa | grep yum-plugin-fastestmirror | sed 's/[ ][ ]*/,/g' )
    if [ -z "$is_yum_plugin_fastestmirror_installed" ]; then
        echo "yuming install yum-plugin-fastestmirror -y ..."
        yum install yum-plugin-fastestmirror -y
    fi
    echo "yum update -y ..."
    yum update -y
fi

